﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Ankha.Core
{
    public sealed class Log
    {
        //Fields  
        private string _FileName;
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        private bool _ShowTimeStamp;
        public bool ShowTimeStamp
        {
            get { return _ShowTimeStamp; }
            set { _ShowTimeStamp = value; }
        }

        private bool _OverwriteIfExists;
        public bool OverwriteIfExists
        {
            get { return _OverwriteIfExists; }
            set { _OverwriteIfExists = value; }
        }

        private string _TimeStampFormat;
        public string TimeStampFormat
        {
            get { return _TimeStampFormat; }
            set { _TimeStampFormat = value; }
        }

        private string _InstanceName;
        public string InstanceName
        {
            get { return _InstanceName; }
            set { _InstanceName = value; }
        }

        //cTor
        public Log(string FileName, string InstanceName = "Log")
        {
            ShowTimeStamp = true;
            TimeStampFormat = "dd:mm:yy hh:mm";
            this.InstanceName = InstanceName;
            this.FileName = FileName;
            CreateFile();
        }

        //Methods
        public void CreateFile()
        {
            using (StreamWriter writer = new StreamWriter(FileName, false))
            {
                writer.WriteLine(InstanceName + " logging started");
            };
        }

        public void WriteString(string str, params object[] formatters)
        {
            Write(string.Format(str, formatters));
        }
        public void Write(object obj)
        {
            using (StreamWriter writer = new StreamWriter(FileName, !OverwriteIfExists))
            {
                string outputStr = "";
                if (ShowTimeStamp)
                {
                    string timeStamp = @"[" + DateTime.Now.ToString(TimeStampFormat) + "]: ";
                    outputStr = timeStamp + obj.ToString();
                }
                else
                    outputStr = obj.ToString();

                writer.WriteLine(outputStr);
            };
        }
    }
}
